package com.rent.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.rent.bean.User;
import com.rent.dao.UserDao;
import com.rent.utils.BaseDao;
/**
 * 用户接口实现类
 * @author xin
 *
 */
public class UserDaoImpl extends BaseDao implements UserDao{

	@Override
	public User sele_nameAndpwd(String name, String pwd) {
		String sql = "select * from user where uname = ? and upassword = ?;";
		User user = null;
		ResultSet rs = executeQuery(sql, name,pwd);
		try {
			if(rs.next()) {
				user = new User(rs.getInt("uid"),rs.getString("uname"),rs.getString("upassword"),rs.getInt("power"),
						rs.getString("idname"),rs.getString("idcard"),rs.getString("phonenumber"), rs.getDouble("balance"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public int add(User u) {
		String sql = "insert into user(uid,uname,upassword,power,idname,idcard,phonenumber) values(null,?,?,1,?,?,?);";
		return executeUpdate(sql, u.getUname(),u.getUpassword(),u.getIdname(),u.getIdcard(),u.getPhonenumber());
	}

	@Override
	public User selectByName(String name) {
		String sql = "select * from user where uname = ?";
		User u = null;
		ResultSet rs = executeQuery(sql, name);
		try {
			if(rs.next()) {
				u = new User(rs.getInt("uid"),rs.getString("uname"),rs.getString("upassword"),rs.getInt("power"),
						rs.getString("idname"),rs.getString("idcard"),rs.getString("phonenumber"), rs.getDouble("balance"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return u;
	}

	@Override
	public int updatePowerByUname(String name) {
		String sql = "update vip set power = 1 where uname = ?;";
		return executeUpdate(sql, name);
	}
	/**
	 * 后台修改用户信息
	 */
	@Override
	public int update(User uid) {
		// TODO Auto-generated method stub
		String sql="update user set upassword=? where uname=?";
		int xiu=super.executeUpdate(sql, uid.getUpassword(),uid.getUname());
		return xiu;
	}

	@Override
	public int updateMoneyByUname(User u) {
		String sql = "update user set balance = ? where uname =?";
		return executeUpdate(sql,u.getBalance(),u.getUname());
	}

	@Override
	public List<User> sele() {
		String sql = "select * from user;";
		List<User> list = new ArrayList<User>();
		ResultSet rs = executeQuery(sql);
		try {
			while(rs.next()) {
				User u = new User(rs.getInt("uid"),rs.getString("uname"),rs.getString("upassword"),rs.getInt("power"),
						rs.getString("idname"),rs.getString("idcard"),rs.getString("phonenumber"),
						rs.getDouble("balance"),rs.getInt("confirminfo"));
				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(rs);
		}
		return list;
	}

	@Override
	public List<User> sele_rent() {
		String sql = "select user.*,count(1) rentCount from user left join rent on user.uname=rent.uname group by rent.uname;";
		List<User> list = new ArrayList<User>();
		ResultSet rs = executeQuery(sql);
		try {
			while(rs.next()) {
				User u = new User(rs.getInt("uid"),rs.getString("uname"),rs.getString("upassword"),rs.getInt("power"),
						rs.getString("idname"),rs.getString("idcard"),rs.getString("phonenumber"),
						rs.getDouble("balance"),rs.getInt("confirminfo"),rs.getInt("rentCount"));
				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(rs);
		}
		return list;
	}
	

	
	
}
